package com.hzqy.commons.utils;

import javax.annotation.Resource;
import static org.quartz.CronScheduleBuilder.cronSchedule;  
import static org.quartz.JobBuilder.newJob;  
import static org.quartz.TriggerBuilder.newTrigger;  
  
  
import org.quartz.CronTrigger;  
import org.quartz.JobDetail;  
import org.quartz.JobKey;
import org.quartz.Scheduler;  
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;  
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;  
  
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CronTriggerUtil {
	protected final static Logger LOG = LoggerFactory.getLogger(CronTriggerUtil.class);
    private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory();  
    /**
     * 创建定时任务
     * @param jobName	
     * @param jobGroupName	
     * @param triggerName
     * @param triggerGroupName
     * @param jobClass
     * @param time
     * @param data
     */
	public static void addJob(String jobName, String jobGroupName,  
            String triggerName, String triggerGroupName, Class jobClass,  
            String time,String message){
		LOG.debug("创建定时任务"+jobName+"time=="+time+"message=="+message);
	    try {
	    	Scheduler sched = gSchedulerFactory.getScheduler();  
			JobDetail job = newJob(jobClass).withIdentity(jobName, jobGroupName)  
	                .build(); 
			  job.getJobDataMap().put("message",message); 
	        CronTrigger trigger = newTrigger().withIdentity(triggerName, triggerGroupName).startNow()
	                .withSchedule(cronSchedule(time)).build();
	       sched.scheduleJob(job, trigger);  //将建立的任务添加进任务团体
		} catch (SchedulerException e) {
			LOG.error("创建失败",e);
		}  
	}
	/**
	 * 修改定时任务【未完成】
	 * @param jobName
	 * @param jobGroupName
	 * @param triggerName
	 * @param triggerGroupName
	 * @param jobClass
	 * @param time
	 * @param data
	 */
	public static void updateJob(String jobName, String jobGroupName,  
            String triggerName, String triggerGroupName, Class jobClass,  
            String time,String message) {  
		LOG.debug("修改定时任务"+jobName+"time=="+time+"message=="+message);
	        try {  
	            Scheduler sched = gSchedulerFactory.getScheduler();  
	           /* CronTrigger trigger = (CronTrigger) sched.getTrigger(new TriggerKey(triggerName));  
	            if (trigger == null) {
	            	LOG.debug("未获取响应任务triggerName=="+triggerName);
	                return;  
	            }  
	            String oldTime = trigger.getCronExpression();  
	            if (!oldTime.equalsIgnoreCase(time)) {  */
	                JobDetail jobDetail = sched.getJobDetail(new JobKey(jobName));  
	                Class objJobClass = jobDetail.getJobClass();  
	                removeJob(jobName,triggerName);  
	               addJob(jobName, jobGroupName,triggerName,triggerGroupName,jobClass, time,message);  
	            /*}  */
	        } catch (Exception e) {  
	            LOG.error("更新定时任务失败",e);  
	        }  
	}
	/**
	 * 删除定时任务
	 * @param jobName
	 * @param triggerName
	 */
	public static void removeJob(String jobName,String triggerName){
		LOG.debug("删除定时任务"+jobName);
		try {  
            Scheduler sched = gSchedulerFactory.getScheduler();  
            sched.pauseTrigger(new TriggerKey(triggerName));// 停止触发器  
            sched.unscheduleJob(new TriggerKey(triggerName));// 移除触发器  
            sched.deleteJob(new JobKey(jobName));// 删除任务  
        } catch (Exception e) {  
            throw new RuntimeException(e);  
        }  
	}
	/**
	 * 启动所有定时任务 
	 */
    public static void startJobs() {  
        try {  
            Scheduler sched = gSchedulerFactory.getScheduler();  
            LOG.debug("成功启动任务组=="+sched.getSchedulerName());
            sched.start();  
        } catch (Exception e) {  
        	LOG.error("任务组启动失败",e);
        }  
    }  
	 /** 
     * @Description:关闭所有定时任务 
     */  
	 public static void shutdownJobs() {  
	        try {  
	            Scheduler sched = gSchedulerFactory.getScheduler();  
	            if (!sched.isShutdown()) { 
	            	LOG.debug("成功关闭任务组"+sched.getSchedulerName());
	                sched.shutdown();  
	            }  
	        } catch (Exception e) {  
	        	LOG.error("任务组关闭失败",e);
	        }  
	   }  
	 /**
	  * 获取任务列表
	  * 需要补全
	  */
	 public static void selectJobs(){
		 
	 }
}